package tp.pr5.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import tp.pr5.Rotation;

public class InstructionsView extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
	private GUIController guic;
	private RobotPanel rbtPan;	
	private JButton moveB;
	private JButton quitB;
	private JButton turnB;
	private JButton pickB;
	private JButton dropB;
	private JButton operB;
	private JComboBox<Rotation> rotaC;
	private JTextField idT;
	/**
	 * Main constructor
	 */
	public InstructionsView(RobotPanel rbtPan, NavigationPanel navPan, GUIController guic){	
		this.guic = guic;
		this.rbtPan = rbtPan;
		this.setLayout(new GridLayout(4,2));
		this.setBorder(new TitledBorder("Instructions"));
		init();
	}
	/**
	 * Creates the visual components of the panel
	 */
	private void init(){
		/*
		 * Creates the components
		 */
		moveB = new JButton("MOVE");
		quitB = new JButton("QUIT");
		turnB = new JButton("TURN");
		pickB = new JButton("PICK");
		dropB = new JButton("DROP");
		operB = new JButton("OPERATE");
		Rotation[] rotations = {Rotation.LEFT,Rotation.RIGHT};
		rotaC = new JComboBox<Rotation>(rotations);
		idT = new JTextField();
		
		/*
		 * Adds the components
		 */
		this.add(moveB);
		this.add(quitB);
		this.add(turnB);
		this.add(rotaC);
		this.add(pickB);
		this.add(idT);
		this.add(dropB);
		this.add(operB);
		
		/*
		 * Handles Events
		 */
		moveB.addActionListener(new ActionListener(){			
			@Override
			public void actionPerformed(ActionEvent arg0) {				
				sendMoveInstruction();				
			}});
		quitB.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				sendQuitInstruction();
				
			}});
		turnB.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				sendTurnInstruction(rotaC.getItemAt(rotaC.getSelectedIndex()));
				
			}});
		pickB.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				sendPickInstruction(idT.getText());
			}});
		dropB.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(rbtPan.getSelectedItemId() != null){
					sendDropInstruction(rbtPan.getSelectedItemId());
				}
			}});
		operB.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(rbtPan.getSelectedItemId() != null){				
					sendOperateInstruction(rbtPan.getSelectedItemId());
				}
			}});
	}
	/**
	 * Sends the controller an order to move
	 */
	protected void sendMoveInstruction() {
		this.guic.notifyMove();
		
	}
	/**
	 * Sends the controller an order to operate the selected item
	 * @param selectedItemId
	 */
	protected void sendOperateInstruction(String selectedItemId) {
		this.guic.notifyOperate(selectedItemId);
	}
	/**
	 * sends the controller an order to drop the selected item
	 * @param selectedItemId
	 */
	protected void sendDropInstruction(String selectedItemId) {
		this.guic.notifyDrop(selectedItemId);		
	}
	/**
	 * Sends the controller an order to pick an item
	 * @param text
	 */
	protected void sendPickInstruction(String text) {
		
		this.guic.notifyPick(text);
		
	}
	/**
	 * Sends the controller an order to quit
	 */
	private void sendQuitInstruction(){
		String message = " Really Quit ? ";
        String title = "Quit";
        int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION){
        	this.guic.notifyQuit();
        }
	}
	/**
	 * Sends the controller the order to create a turn instruction
	 * @param itemAt
	 */
	private void sendTurnInstruction(Rotation rot) {
		this.guic.notifyTurn(rot);
	}
}
	

